<template>
  <div class="user-profile">
    <!-- 导航栏 -->
    <van-nav-bar
      class="app-nav-bar"
      title="个人信息"
      left-arrow
      @click-left="$router.back()"
    >
    </van-nav-bar>

    <input
      type="file"
      hidden
      accept="image/*"
      ref="file"
      @change="onFileChange"
    />
    <van-cell title="头像" is-link center @click="$refs.file.click()">
      <van-image width="30" height="30" round fit="cover" :src="user.photo" />
    </van-cell>
    <van-cell
      title="昵称"
      is-link
      :value="user.name"
      @click="isEditNameShow = true"
    ></van-cell>
    <van-cell
      title="性别"
      is-link
      :value="user.gender === 0 ? '男' : '女'"
      @click="isEditGenderShow = true"
    ></van-cell>
    <van-cell
      title="生日"
      is-link
      :value="user.birthday"
      @click="isEditBirthdayShow = true"
    ></van-cell>

    <!-- 修改昵称 -->
    <van-popup
      v-model="isEditNameShow"
      position="bottom"
      :style="{ height: '100%' }"
    >
      <!-- 
      当你传递给子组件的数据既要使用又要修改，例如这里的name
      这种情况下我们可以使用v-model简写
      <update-name
        :name="user.name"
        @update-name="user.name = $event"
        @close="isEditNameShow = false"
      ></update-name>
     -->
      <!-- 
       v-model="user.name"
        默认传递一个叫value的数据
        :value="user.name" 
        默认监听input事件
        @input="user.name=$event"
      v-model的本质还是父子组件通信，它仅仅是简化了父组件的使用
      -->
      <!-- 
        v-model只能使用一次
        如果有多个数据需要保持同步，使用.sync修饰符
        :gender.sync="user.gender"相当于
          :gender="user.gender"
          @input:gender="user.gender=$event"
       -->
      <update-name
        v-if="isEditNameShow"
        v-model="user.name"
        @close="isEditNameShow = false"
      ></update-name>
    </van-popup>

    <!-- 修改性别 -->
    <van-popup v-model="isEditGenderShow" position="bottom">
      <update-gender
        v-model="user.gender"
        @close="isEditGenderShow = false"
      ></update-gender>
    </van-popup>

    <!-- 修改生日 -->
    <van-popup v-model="isEditBirthdayShow" position="bottom">
      <update-birthday
        v-if="isEditBirthdayShow"
        v-model="user.birthday"
        @close="isEditBirthdayShow = false"
      ></update-birthday>
    </van-popup>

    <!-- 修改头像 -->
    <van-popup
      class="update-photo-popup"
      v-model="isEditPhotoShow"
      position="bottom"
      :style="{ height: '100%' }"
    >
      <update-photo
        v-if="isEditPhotoShow"
        :file="previewImage"
        @close="isEditPhotoShow = false"
        @update-photo="user.photo = $event"
      ></update-photo>
    </van-popup>
  </div>
</template>

<script>
import { getUserProfile } from "@/api/user";
import UpdateName from "./components/update-name.vue";
import UpdateGender from "./components/update-gender.vue";
import UpdateBirthday from "./components/update-birthday.vue";
import UpdatePhoto from "./components/update-photo.vue";
export default {
  // 组件名称
  name: "UserProfile",
  // 组件参数 接收来自父组件的数据
  props: {},
  // 局部注册的组件
  components: {
    "update-name": UpdateName,
    "update-gender": UpdateGender,
    "update-birthday": UpdateBirthday,
    "update-photo": UpdatePhoto,
  },
  // 组件状态值
  data() {
    return {
      user: {}, //用户数据
      isEditNameShow: false, //编辑昵称的显示状态
      isEditGenderShow: false, //编辑性别的显示状态
      isEditBirthdayShow: false, //编辑生日的显示状态
      isEditPhotoShow: false, //编辑用户头像的显示状态
      previewImage: null, //选中的预览图片
    };
  },
  // 计算属性
  computed: {},
  // 侦听器
  watch: {},
  created() {
    this.loadUserProfile();
  },
  mounted() {},
  // 组件方法
  methods: {
    async loadUserProfile() {
      const { data } = await getUserProfile();
      this.user = data.data;
    },
    onFileChange() {
      //预览图片
      const file = this.$refs.file.files[0];
      /* const blob = window.URL.createObjectURL(this.$refs.file.files[0]); */
      this.previewImage = file;

      //展示弹出层
      this.isEditPhotoShow = true;

      //为了解决校内沟通文件不处罚 change 事件，所以手动清空 file 的 value
      this.$refs.file.value = "";
    },
  },
};
</script>

<style scoped lang='less'>
.van-popup {
  background-color: #f5f7f9;
}
.update-photo-popup {
  background-color: #000000;
}
</style>